package com.example.demo1.controller;

import com.alibaba.fastjson.JSON;
import com.example.demo1.Dao.GoodsDao;
import com.example.demo1.pojo.Goods;
import lombok.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

@RestController
public class GoodsController {
    @Autowired
    GoodsDao goodsDao;
    @PostMapping("addGoods")
    public boolean test1(@RequestBody Goods goods)
    {
        return goodsDao.addGoods(goods);
    }

    @GetMapping("loadGoods")
    public List<Goods> loadAllGoods()
    {
        var lis = goodsDao.loadAllGoods();
        return lis;
    }

    @PutMapping("updateGoods")
    public boolean updateGoods(@RequestBody String json)
    {
        Map maps = JSON.parseObject(json);
        String goodsName = maps.get("goodsName").toString();
        String updatePrice = maps.get("updatePrice").toString();
        System.out.println("更新货物名称" + goodsName);
        return goodsDao.updateGoods(goodsName,updatePrice);
    }

    @DeleteMapping("deleteGoods")
    public boolean deleteGoodsByName(@RequestBody Map<String,String> mp)
    {
        return goodsDao.deleteGoodsByName(mp.get("name"));
    }

    @GetMapping("hello")
    public String hello()
    {
        return "Hello, World";
    }

}
